// add/addFacility/addFacility.js
import http from '../../http'
import gcoord from 'gcoord'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		projectId: '',
		groups: null,
		name: '',
		sn: '',
		typeId: '',
		longitude: '',
		latitude: '',
		property: '',
		status: '0',

		markers: [{
			longitude: '',
			latitude: '',
			id: 1,
			iconPath: '../../img/map/lamp-post.png',
			width: 20,
			height: 20,
		}],
		type: '普通灯杆',
		typeShow: false,
		typeList: [{
			text: '灯杆',
			value: '1'
		}, {
			text: '配电箱',
			value: '2'
		}],

	},
	coord(location) {
		var result = gcoord.transform(
			location, // 经纬度坐标
			gcoord.BD09, // 当前坐标系
			gcoord.GCJ02 // 目标坐标系
		);
		return result
	},
	coord1(location) {
		var result = gcoord.transform(
			location, // 经纬度坐标
			gcoord.GCJ02, // 当前坐标系
			gcoord.BD09 // 目标坐标系
		);
		return result
	},
	// 灯杆分类选择
	typeClick() {
		this.setData({
			typeShow: true
		})
	},
	typeConfirm(event) {
		this.setData({
			type: event.detail.value.text,
			typeId: event.detail.value.value,
			typeShow: false
		})
	},
	typeClose() {
		this.setData({
			typeShow: false
		})
	},
	// 获取自身位置
	myPosition() {
		wx.getLocation({
			type: "gcj02",
			success: (res) => {
				console.log(res);
				this.setData({
					longitude: res.longitude,
					latitude: res.latitude
				})
			},
			fail(error) {
				console.log(error);
			}
		})
	},
	// 地图选点
	mapClick() {
		let markers = this.data.markers
		markers[0].longitude = this.data.longitude
		markers[0].latitude = this.data.latitude
		this.setData({
			markers: markers,
			mapBol: true
		}, () => {
			console.log(this.data.markers);
		})
	},
	mapTap(event) {
		console.log(event.detail);
		let markers = this.data.markers
		markers[0].longitude = event.detail.longitude
		markers[0].latitude = event.detail.latitude
		this.setData({
			markers: markers
		})
	},
	mapConfirm() {
		this.setData({
			longitude: this.data.markers[0].longitude,
			latitude: this.data.markers[0].latitude
		})
	},
	mapClose() {
		this.setData({
			mapBol: false
		})
	},
	addLamp() {
		wx.showLoading({
			title: '加载中...'
		});
		let form = {
			projectId: this.data.projectId,
			groups: this.data.groups,
			name: this.data.name,
			sn: this.data.sn,
			typeId: this.data.typeId,
			longitude: this.coord1([this.data.longitude, this.data.latitude])[0],
			latitude: this.coord1([this.data.longitude, this.data.latitude])[1],
			property: this.data.property,
			remarks: this.data.remarks,
			status: this.data.status,
		}
		for (const key in form) {
			if (form[key] != 0 && !form[key]) {
				delete form[key]
			}
		}
		http.post({
			url: '/zg/light-pole/create',
			data: form,
			then: (res) => {
				console.log('灯杆添加设备', res);
				if (res.data.code == 0) {
					wx.showToast({
						title: '添加成功',
						icon: 'success',
					})
					wx.navigateBack({
						delta: 1
					});
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		let pages = getCurrentPages()
		console.log(pages);
		let currentPage = pages[pages.length - 1]
		let options = currentPage.options

		http.get({
			url: '/zg/project/get',
			data: {
				id: options.projectId,
			},
			then: (res) => {
				console.log('项目信息', res);
				this.setData({
					projectId: res.data.data.parentId == 0 ? res.data.data.id : res.data.data.parentId,
					groups: res.data.data.parentId == 0 ? null : res.data.data.id,
					longitude: this.coord([res.data.data.longitude, res.data.data.latitude])[0],
					latitude: this.coord([res.data.data.longitude, res.data.data.latitude])[1],
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})